﻿
(function () {

    appModule.controller('tenant.views.parameterType.index', [
        '$scope', '$uibModal', 'uiGridConstants', 'abp.services.app.parameterType',
        function ($scope, $modal, uiGridConstants, parameterTypeService) {
            var vm = this;

            $scope.$on('$viewContentLoaded', function () {
                Metronic.initAjax();
            });

            vm.loading = false;
            vm.filterText = null;
            vm.curParameterType = null;
            vm.mainGrid = null;
            vm.permissions = {
                create: abp.auth.hasPermission('Pages.Tenant.ParameterType.Create'),
                edit: abp.auth.hasPermission('Pages.Tenant.ParameterType.Update'),
                'delete': abp.auth.hasPermission('Pages.Tenant.ParameterType.Delete')
            };
            
            var urlParameterType = "/api/services/app/parameterType/";

            var mainGridDataSource = new kendo.data.DataSource({
                type: 'aspnetmvc-ajax',
                transport: {
                    read: {
                        url: urlParameterType + "KendoGetParameterTypes",
                        contentType: "application/json",
                        type: "POST"
                    },
                    parameterMap: function (options, operation) {
                        return JSON.stringify(options);
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.data;
                        }
                        return {};
                    },
                    total: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.total;
                        }
                        return 0;
                    },
                    model: {
                        id: "id",
                        fields: {
                            id: { type: "number" },
                        }
                    }
                },
                filter: {
                    logic: "and",
                    filters: [
                        { field: "code", operator: "eq", value:"1" }
                    ]
                }
            });
            vm.codeVal = '1';
            vm.Handover = function (n) {
                vm.codeVal = n.toString();
                var filter = {
                    logic: "and",
                    filters: [
                       { field: "code", operator: "eq", value: n.toString() }
                    ]
                }
                vm.mainGrid.dataSource.filter(filter);
            };

            //var filter = {
            //    logic: "and",
            //    filters: [
            //       { field: "titleTableId", operator: "eq", value: vm.curTitleTableId },
            //       { field: "endTime", operator: "lte", value: vm.endTime() },
            //       { field: "beginTime", operator: "gte", value: vm.startTime() },
            //       { field: "ReleaseStatus", operator: "eq", value: "发布" },
            //    ]
            //}
            //vm.mainGrid.dataSource.filter(filter);


            vm.mainGridOptions = {
                dataSource: mainGridDataSource,
                columnMenu: true,
                reorderable: true,
                resizable: true,
                filterable: true,
                scrollable: true,
                selectable: "multiple",
                allowCopy: true,
                //height: 410,
                change: function (e) {
                    var selectedRows = this.select();
                    for (var i = 0; i < selectedRows.length; i++) {
                         vm.curParameterType = this.dataItem(selectedRows[i]);
                    }
                },
                pageable: {
                    buttonCount: 5.0,
                    pageSize: 10,
                    pageSizes: [10, 20, 50, 100],
                    refresh: true
                },
                sortable: {
                    mode: "multiple"
                },
                messages: {
                    commands: {
                        excel: "导出"
                    }
                },
                columns: [ 
                    {
                        title: '名称 ',
                        field: 'name',
                        width: 100
                    },
                ]
            };

            
            function openCreateOreditParameterTypeModal(parameterTypeId) {
                var modalInstance = $modal.open({
                    templateUrl: '~/App/tenant/views/parametertype/createOrEditParameterType.cshtml',
                    controller: 'tenant.views.parametertype.createOrEditParameterType as vm',
                    backdrop: 'static',
                    resolve: {
                        parameterTypeId: function () {
                            return parameterTypeId;
                        },
                        code: function () {
                            return vm.codeVal;
                        }
                    }
                });

                modalInstance.result.then(function (result) {
                    vm.mainGridOptions.dataSource.read();
                });
            }
            vm.editParameterType = function () {
                if (vm.curParameterType) {
                    openCreateOreditParameterTypeModal(vm.curParameterType.id);
                } else {
                    abp.message.error("请选择要修改的行!");
                }
            };

            vm.createParameterType = function () {
                openCreateOreditParameterTypeModal(null);
            };

            vm.deleteParameterType = function () {
                if (vm.curParameterType != null && vm.curParameterType.id > 0) {
                    abp.message.confirm(
                        "是否要删除此条记录！",
                        function (isConfirmed) {
                            if (isConfirmed) {
                                parameterTypeService.deleteParameterType({ id: vm.curParameterType.id }).success(function () {
                                    abp.notify.info(app.localize('SavedSuccessfully'));
                                    vm.mainGridOptions.dataSource.read();
                                    vm.curParameterType = null;
                                }).finally(function () {

                                });
                            }
                        }
                    );
                } else {
                    abp.message.error("请选择要删除的行!");
                }
            };

        }]);
})();
